package cn.com.bsoft.cloud.dao.cloud;

import cn.com.bsoft.cloud.UseDatasourceCloud;
import cn.com.bsoft.cloud.entity.AppointFileMess;
import cn.com.bsoft.cloud.entity.cloud.*;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.net.URI;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
@UseDatasourceCloud
public interface CloudAppDao {
    
    @Select("select  b.psdbm siteCode,b.psdmc siteName,a.appointId,a.appointDate,a.hospitalCode,a.hospitalName,a.departmentCode,a.departmentName,\n" +
            "  a.patientName,a.identity,a.sex,a.appointStartTime,a.appointEndTime,a.appointStatus\n" +
            "  ,a.createtime,a.modifytime from t_prescription_appointment a\n" +
            " LEFT JOIN t_appoint_site b\n" +
            "ON (a.appointId=b.yyid) where a.appointId=#{yyid}")
    AppointMessSec queryDrugByYyid(@Param("yyid") String yyid);

    @Results({
            @Result(property="siteAddress",column="address")
    })
    @Select("select  c.address,b.psdbm siteCode,b.psdmc siteName,a.appointId,a.appointDate,a.hospitalCode,\n" +
            "a.hospitalName,a.departmentCode,a.departmentName,\n" +
            "a.patientName,a.identity,a.sex,a.appointStartTime,a.appointEndTime,a.appointStatus\n" +
            ",a.createtime,a.modifytime from t_prescription_appointment a\n" +
            "LEFT JOIN t_appoint_site b\n" +
            "ON (a.appointId=b.yyid)\n" +
            "LEFT JOIN t_distribution_site c\n" +
            "ON (b.psdbm=c.psdbm and c.yydm=a.hospitalCode)\n" +
            "where a.appointId in (${yyidstr})")
    List<AppointMessSec> queryDrugByYyidSec(@Param("yyidstr") String yyidstr);

    @Results({@Result(property = "prescriptionCode",column = "prescriptionCode"),
            @Result(property = "cfmess",javaType = List.class,column ="prescriptionCode",
                    many = @Many(select = "cn.com.bsoft.cloud.dao.cloud.CloudCompanyDao.queryDetailByCode"))})
    @Select("select pharmacyCode,pharmacyName,hospitalCode ,hospitalName ,\n" +
            "prescriptionCode,patientName,cardNum,extractingDate,identity,sex,telphone\n" +
            "from t_prescription_drug_list\n" +
            "where\n" +
            "deliveryStatus='0'\n" +
            "and appointId=#{yyid}")
    List<PrescriptionMess> queryDrugDetailByYyid(@Param("yyid") String yyid);


    @Select("select drugCode,drugName,specificationCode,specification,dispenseAmount,dispenseUnitsCode,\n" +
            "dispenseUnitsName,frequencyCode,frequencyName,aDose,doseUnitCode,doseUnitName,formulationCode,\n" +
            "formulationName,useDescribe,medicationDay,routeCode,routeName,unitPrice,financialTransaction\n" +
            "from t_prescription_drug_detail \n" +
            "where prescriptionCode=#{prescriptionStr}")
    List<PrescriptionDetail> queryDetailByCode(@Param("prescriptionCode") String prescriptionCode);

    @Results({@Result(property = "prescriptionCode",column = "prescriptionCode"),
            @Result(property = "cfmess",javaType = List.class,column ="prescriptionCode",
                    many = @Many(select = "cn.com.bsoft.cloud.dao.cloud.CloudCompanyDao.queryDetailByCode"))})
    @Select("select deliveryStatus,pharmacyCode,pharmacyName,hospitalCode ,hospitalName ,\n" +
            "prescriptionCode,patientName,cardNum,extractingDate,identity,sex,telphone,paymentStatus\n" +
            "from t_prescription_drug_list\n" +
            "where\n" +
            "deliveryStatus in ('0','1','2')\n" +
            "and appointId=#{yyid}")
    List<PrescriptionMessSec> queryDrugDetailByYyidSec(@Param("yyid") String yyid);

    @Select("select yyid from t_appoint_site where yyid=#{yyid}")
    String findAppointId(@Param("yyid") String yyid);

    @Insert("INSERT into t_appoint_site(yyid,psdbm,psdmc) values(#{yyid},#{psdbm},#{psdmc})")
    Boolean addAppointSite(@Param("yyid") String yyid,@Param("psdbm") String psdbm,@Param("psdmc") String psdmc);

    @Select("select logName,medicalInstitutionCode,medicalInstitutionName,\n" +
            "  doctorName,workNumber,identityCard,gender,departmentName,departmentCode from t_doctor_information\n" +
            "where logName=#{logName}")
    List<DoctorInformation> findMessByLogname(@Param("logName") String logName);

    @Insert("insert into t_file_mess(yyid,url) values(#{yyid},#{uri})")
    Boolean insertFileurl(@Param("yyid") String yyid,@Param("uri") String uri);

    @Insert("insert into t_upload_files(recordId,uploadUrl,patientName,identifyNo,cardNo,cardType,hospitalCode,doctorNo,doctorName,loginName,createTime)\n" +
            "values(#{param.recordId},#{url},#{param.patientName},#{param.identifyNo},#{param.cardNo},#{param.cardType},#{param.hospitalCode},#{param.doctorNo},#{param.doctorName},#{param.loginName},now())")
    Boolean addAppointFileMess(@Param("param") AppointFileMess param,@Param("url") String url);

    @Select("select recordId,uploadUrl,patientName,identifyNo,cardNo,doctorName from t_upload_files ${paramstr}")
    List<UrlMess> queryUrl(@Param("paramstr") String paramstr);

    @Insert("insert into t_doctor_information(logName,medicalInstitutionName,\n" +
            "medicalInstitutionCode,doctorName,workNumber,identityCard,gender,departmentName,departmentCode)\n" +
            "values(#{userMess.loginName},#{hosName},#{hosCode},#{dmess.doctorName},#{workNum},#{idCard},#{dmess.doctorGender},#{dmess.departmentName},#{dmess.departmentCode})")
    Boolean addDocMess(@Param("hosCode") String hosCode, @Param("hosName") String hosName,@Param("idCard") String idCard,@Param("workNum") String workNum,@Param("userMess") UserMess userMess, @Param("dmess") DoctorMess dmess);

    @Select("select departmentCode from t_cloud_partment where hospitalCode=#{hosCode}")
    ArrayList<String> findDepartListByCode(@Param("hosCode") String hosCode);

    @Select("select hospitalCode,departmentCode,doctorNum from t_cloud_partment ${paramstr}")
    List<CloudDoctor> queryCloudDoctor(@Param("paramstr") String paramstr);

    @Select("select hospitalCode from t_cloud_partment ${paramstr} group by hospitalCode")
    List<String> queryListcode(@Param("paramstr") String paramstr);
}
